package cn.com.bluemoon.daps.model.core.file.hive;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.validation.illegal.IllegalChar;
import cn.com.bluemoon.daps.model.core.file.TableData;
import cn.com.bluemoon.daps.model.core.file.excel.converter.MyHiveSerdeAsIntegerStringConverter;
import cn.com.bluemoon.daps.model.core.file.excel.converter.MyWdTableCategoryIntegerStringConverter;
import cn.com.bluemoon.daps.model.core.file.validator.MyTopicCategory;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * <pre>
 *
 * </p>   序号	表英文名	表中文名	表业务描述	标签	组路径	主题	表分类	stored as	SERDEPROPERTIES	TBLPROPERITIES
 * </p>1	b2b_User_Huanxin_Info	用户环信信息表			ODS/XXX组	一级主题/二级主题/三级主题	维表	PARQUET
 *
 * </pre>
 *
 * @author Jarod.Kong
 * @date 2021/2/5 12:01
 */
@Data
@ColumnWidth(20)
@HeadRowHeight(15)
@ContentRowHeight(20)
public class HiveTableData extends TableData {

    // common props
    @ExcelProperty("序号")
    @ColumnWidth(8)
    private Integer order;

    //    @NoSqlKeyword(dbtype = DbType.hive, name = "表英文名")
    @IllegalChar(message = "表英文名存在非法字符")
    @ExcelProperty("表英文名")
    @Size(max = 64, message = "表英文名不可以大于{max}")
    @NotNull(message = "表英文名不可为空")
    private String tableName;

    @ExcelProperty("表中文名")
    @Size(max = 2048, message = "表中文名不可以大于{max}")
    @NotNull(message = "表中文名不可为空")
    private String tableNameZh;

    @ExcelProperty("Schema")
    @Size(max = 32, message = "Schema不可以大于{max}")
    private String schema;

    @Size(max = 1024, message = "业务描述不可以大于{max}")
    @ExcelProperty("表业务描述")
    private String bizDesc;

    // @MyTableGroup
    @ExcelProperty(value = "组路径")
    @NotNull(message = "组路径不可为空")
    private String groupPathName;

    // hive props
    @MyTopicCategory
    @ExcelProperty("主题")
    @NotNull(message = "主题不可为空")
    private String topicName;
    /**
     * 1 表示维表，2 为事实表，3 贴源表
     */
    @ExcelProperty(value = "表分类", converter = MyWdTableCategoryIntegerStringConverter.class)
    @NotNull(message = "表分类不可为空")
    private Integer hiveTableCategory;

    @ExcelProperty(value = "stored as", converter = MyHiveSerdeAsIntegerStringConverter.class)
    @NotNull(message = "表数据格式不可为空")
    private Integer hiveStoredAs;

    @ExcelProperty("SERDEPROPERTIES")
    private String hiveSerdeProps;

    @ExcelProperty("TBLPROPERITIES")
    private String hiveTableProps;

    @ExcelProperty("LOCATION")
    @NotNull(message = "表存储路径不可为空")
    private String hiveLocation;

    public static HiveTableData getInstance(){
        return new HiveTableData();
    }
    /**
     * 用于转换为dao模型类
     *
     * @param modelTable 转为
     * @return
     */
    @Override
    protected ResultBean<DapDataModelTable> convertToModel0(DapDataModelTable modelTable) {
        modelTable.setTableCategory(getHiveTableCategory());
        modelTable.setHiveExternal(null);
        modelTable.setHiveTableProps(getHiveTableProps());
        modelTable.setHiveSerdeProps(getHiveSerdeProps());
        modelTable.setHiveStoredAs(getHiveStoredAs());
        modelTable.setHiveLocation(getHiveLocation());
        modelTable.setTableSchema(getSchema());
        return ResultBean.ok(modelTable);
    }

    /**
     * 用于数据库表模型转为导出模型
     *
     * @param table 据库表模型
     */
    @Override
    public HiveTableData copyDbProperties0(DapDataModelTable table) {
        super.copyDbProperties0(table);
        setTableName(table.getEngName());
        setTableNameZh(table.getName());
        setBizDesc(table.getBusinessDesc());
        setHiveTableCategory(table.getTableCategory());
        setHiveStoredAs(table.getHiveStoredAs());
        setHiveSerdeProps(table.getHiveSerdeProps());
        setHiveTableProps(table.getHiveTableProps());
        setHiveLocation(table.getHiveLocation());
        setSchema(table.getTableSchema());
        return this;
    }
}
